<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:09:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceAmountoddTypeRepository;

/**
 * @property FinanceAmountoddTypeRepository $repository 
 */
class AmountoddTypeService extends Service
    {
    public function boot(FinanceAmountoddTypeRepository $repository)
        {
        $this->repository = $repository;
        }
    public function list()
        {
        $data = $this->repository->condition()->orderBy("sort", "asc")->getGrid();
        return $data;
        }
    protected function save()
        {
        $gdata = $this->repository->autosave(request()->toArray());
        if ($gdata->sort == 0) {
            $gdata->sort = $gdata->id;
            $gdata->save();
            }
        return $gdata;
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != -1) {
                fail("类型已{$gdata->status->name}！不能启用！");
                }
            return enable($gdata);

            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("类型已{$gdata->status->name}！不能禁用！");
                }
            return enable($gdata, false);
            });
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != -1) {
                fail("类型已{$v->status->name}！禁止删除");
                }
            return $v->delete();
            });
        }
    }